package com.snap.vseries.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * Description:
 * Data: 2018/9/30
 *
 * @author: cqian
 */
public class DoubleUtils {
    private static final int DEF_DIV_SCALE = 11;

    /**
     * * 两个Double数相加 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double add(Double v1, Double v2, int keep) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.add(b2).setScale(keep, ROUND_HALF_UP).doubleValue());
    }

    public static Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.add(b2).doubleValue());
    }

    public static String add(String v1, String v2) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.add(b2).toPlainString();
    }

    public static String add(String v1, String v2, String v3) {
        return add(v3, add(v1, v2));
    }

    public static String addToStirng(Double v1, Double v2) {
        return addToStirng(v1.toString(), v2.toString());
    }

    public static String addToStirng(String v1, String v2) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.add(b2).toString();
    }

    /**
     * * 两个Double数相减 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double sub(Double v1, Double v2, int keep) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.subtract(b2).setScale(keep, ROUND_HALF_UP).doubleValue());
    }

    public static Double sub(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.subtract(b2).doubleValue());
    }

    public static String sub(String v1, String v2) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).toString();
    }

    public static String subToString(Double v1, Double v2) {
        return subToString(v1.toString(), v2.toString());
    }

    public static String subToString(String v1, String v2) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).toString();
    }


    /**
     * 将每三个数字加上逗号处理（通常使用金额方面的编辑）
     *
     * @param str 无逗号的数字
     * @return 加上逗号的数字
     */

    public static String addComma(String str) {

        if (str == null || ".".equals(str)) return "";
        String endStr = "";//小数点后面的数字
        if (str.contains(".") && !str.endsWith(".")) {
            int index = str.indexOf(".");
            if (index >= 0) {
                endStr = str.substring(index + 1, str.length());
                str = str.substring(0, index);
            }
        }
        // 将传进数字反转

        String reverseStr = new StringBuilder(str).reverse().toString();

        String strTemp = "";

        for (int i = 0; i < reverseStr.length(); i++) {

            if (i * 3 + 3 > reverseStr.length()) {

                strTemp += reverseStr.substring(i * 3, reverseStr.length());

                break;

            }

            strTemp += reverseStr.substring(i * 3, i * 3 + 3) + ",";

        }

        // 将[789,456,] 中最后一个[,]去除

        if (strTemp.endsWith(",")) {

            strTemp = strTemp.substring(0, strTemp.length() - 1);

        }

        // 将数字重新反转

        String resultStr = new StringBuilder(strTemp).reverse().toString();
        if (TextUtils.isEmpty(endStr)) {
            return resultStr;
        } else {
            return resultStr + "." + endStr;
        }

    }

    /**
     * * 两个Double数相乘 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double mul(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.multiply(b2).doubleValue());
    }

    public static String mul(String v1, String v2) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).toPlainString();
    }

    public static Double mul(Double v1, Double v2, int totalLength) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        BigDecimal multiply = b1.multiply(b2);
        String result = multiply.toString();
        if (!TextUtils.isEmpty(result)) {
            if (result.length() > totalLength) {
                result = result.substring(0, totalLength);
            }
            return Double.valueOf(result);
        } else {
            return 0d;
        }
    }

    /**
     * * 两个Double数相除 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double div(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.divide(b2, DEF_DIV_SCALE, ROUND_HALF_UP)
                .doubleValue());
    }

    public static String div(String v1, String v2) {
        if (TextUtils.isEmpty(v1) || TextUtils.isEmpty(v2)) return "";
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2, DEF_DIV_SCALE, RoundingMode.DOWN).toPlainString();
    }

    public static Double div(int totalLength, Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());


        String result = b1.divide(b2, DEF_DIV_SCALE, ROUND_HALF_UP).toString();
        if (!TextUtils.isEmpty(result)) {
            if (result.length() > totalLength) {
                result = result.substring(0, totalLength);
            }
            return Double.valueOf(result);
        } else {
            return 0d;
        }
    }

    /**
     * 有小数情况下去除尾部0,去除首部0
     *
     * @param data
     * @return
     */
    public static String replaceZero(String data) {
        if (TextUtils.isEmpty(data)) {
            return data;
        }

        if (data.contains(".")) {
            while (data.endsWith("0")) {
                data = data.substring(0, data.length() - 1);
            }
        } else {
            boolean temp = false;
            for (int i = 0; i < data.length(); i++) {
                if (!"0".equals(data.charAt(i) + "")) {
                    temp = true;
                    break;
                }
            }
            if (!temp) {
                //data全为0
                return "0";
            }
        }

        if (data.endsWith(".")) {
            data = data.substring(0, data.length() - 1);
        }

        //去除首部0
        while ((data.contains(".") && data.startsWith("00")) || (!data.contains(".") && data.startsWith("0") && data.length() > 1)) {
            data = data.substring(1, data.length());
        }
        return data;
    }

    /**
     * * 两个Double数相除，并保留scale位小数 *
     *
     * @param v1    *
     * @param v2    *
     * @param scale *
     * @return Double
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.divide(b2, scale, ROUND_HALF_UP).doubleValue());
    }

    /**
     * 将double格式化为指定小数位的String，取下限
     *
     * @param v     需要格式化的数字
     * @param scale 小数点后保留几位
     * @return
     */
    public static String roundByScale(double v, int scale) {
        if (scale < 0)
            return v + "";
        if (isCorrect(v, scale)) {
            return v + "";
        } else {
            BigDecimal bigDecimal = new BigDecimal(v);
            return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
        }
    }

    /**
     * 在指定精度内不作处理,超出指定精度长度做舍弃处理
     *
     * @param v
     * @param scale
     * @return
     */
    public static String roundByScale(String v, int scale) {
        if (scale < 0) {
            return v;
        }
        if (isCorrect(v, scale)) {
            return v;
        } else {
            if (!TextUtils.isEmpty(v) && v.contains(".") && !v.endsWith(".")) {
                try {
                    int index = v.indexOf(".") + 1;
                    v = v.substring(0, index + scale);
                } catch (Exception e) {
                }
            }
            return v;
        }
    }

    public static String roundByScale(String v, String scaleString) {
        int scale = -1;
        try {
            scale = Integer.valueOf(scaleString);
        } catch (Exception e) {
        }
        if (scale >= 0) {
            return roundByScale(v, scale);
        }
        return v;
    }

    /**
     * 当前文本是否在  指定小数位内
     *
     * @param v
     * @param scale
     * @return
     */
    public static boolean isCorrect(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The   scale   must   be   a   positive   integer   or   zero");
        }
        return isCorrect(v + "", scale);
    }

    public static boolean isCorrect(String v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The   scale   must   be   a   positive   integer   or   zero");
        }
        int currentScale = 0;
        String text = v + "";
        if (!text.contains(".")) {
            return true;
        } else if (text.endsWith(".")) {
            return false;
        } else {
            currentScale = text.length() - text.indexOf(".") - 1;
            return currentScale <= scale;
        }
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * a是否比b大
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean above(String a, String b) {
        try {
            BigDecimal aDecimal = new BigDecimal(a);
            BigDecimal bDecimal = new BigDecimal(b);
            return aDecimal.compareTo(bDecimal) == 1;
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * a是否比b小
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean below(String a, String b) {
        try {
            BigDecimal aDecimal = new BigDecimal(a);
            BigDecimal bDecimal = new BigDecimal(b);
            return aDecimal.compareTo(bDecimal) == -1;
        } catch (Exception e) {
        }
        return false;
    }
}
